package thread;

/**
 * @description: 同步方法演示
 * @author: apple
 * @date:  2021/11/27 10:59 下午
 * @version 1.0
 **/
public class SynchronizedMethod {

    public static void main(String[] args) {

        StationWindow2 data = new StationWindow2();

        Thread t1 = new Thread(data , "第一个窗口");
        Thread t2 = new Thread(data , "第二个窗口");
        Thread t3 = new Thread(data , "第三个窗口");

        t1.start();
        t2.start();
        t3.start();
    }
}

class StationWindow2 implements Runnable {
    private int ticket = 100;

    @Override
    public void run() {
        while (true) {
            if (extracted()) break;
        }
    }


    private synchronized boolean extracted() {  // 同步监视器是this，如果继承方式的话，这个方法必须是静态方法
        if (ticket > 0) {
            System.out.println(Thread.currentThread().getName() + "票号：" + ticket);
            ticket--;
        } else {
            return true;
        }
        return false;
    }
}
